﻿using Demo.Domains.Entities.Sys;
using Demo.PetaPoco.Application.DbContexts;
using Demo.PetaPoco.Application.Dtos.Dept;
using Microsoft.Extensions.DependencyInjection;
using PetaPoco;
using PmSoft.Core;
using PmSoft.Data.Abstractions;
using PmSoft.Data.PetaPoco.Repositories;

namespace Demo.PetaPoco.Application.Services.Dept;

[Inject(ServiceLifetime.Scoped)]
public class DeptRepository : CacheRepository<AppDbContext, SysDept, int>
{
	public DeptRepository(AppDbContext dbContext,
		IApplicationContext applicationContext)
		: base(dbContext, applicationContext)
	{

	}

	internal async Task<IPagedList<SysDept>> GetPagedListAsync(DeptPageQuery query)
	{
		Sql sql = Sql.Builder
	  .Select("id")
	  .From("sys_dept");

		if (!string.IsNullOrWhiteSpace(query.DeptName))
			sql.Where("name like @0", "%" + query.DeptName + "%");

		if (query.ProvinceNum.HasValue)
			sql.Where("province_num=@0", query.ProvinceNum);

		if (query.CityNum.HasValue)
			sql.Where("city_num=@0", query.CityNum);

		if (query.RegionNum.HasValue)
			sql.Where("region_num=@0", query.RegionNum);

		sql.OrderBy("id asc");

		return await GetPagedEntitiesAsync(sql,
		   query.PageIndex,
		   query.PageSize
		   );
	}
}
